﻿// Author:					HungNM
// Created:					17-08-2012
// Last Modified:			17-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{

    public partial class AddUserPage : System.Web.UI.Page
    {
        private string UserName = string.Empty;
        private string Password = string.Empty;
        private Users user;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            try
            {
                user = new Users();
                user.UserName = txtUserName.Text.Trim().ToString();
                user.Password = txtPassword.Text.Trim().ToString();
                user.FirstName = txtFirstName.Text.Trim().ToString();
                user.LastName = txtLastName.Text.Trim().ToString();
                user.DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text.Trim().ToString());
                user.Email = txtEmail.Text.Trim().ToString();
                user.Address = txtAddress.Text.Trim().ToString();
                user.Gender = (Convert.ToInt32(rdGender.SelectedValue) > 0);
                user.PhoneNumber = txtPhoneNumber.Text.Trim().ToString();
                user.Roles = 1;
                user.Save();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                user = null;
            }
        }


        private void PopulateLabels()
        {
            if (IsPostBack) return;
            Title = "Add User Page - eProject";
            ltHeader.Text = "Create User";
            lblUser.Text = "User Name";
            lblPassword.Text = "Password";
            lblConfirmPassword.Text = "Confirm Password";
            lblEmail.Text = "Email";
            lblFirstName.Text = "First Name";
            lblLastName.Text = "Last Name";
            lblDateOfBirth.Text = "Date Of Birth";
            lblGender.Text = "Gender";
            lblAddress.Text = "Address";
            lblPhoneNumber.Text = "Phone Number";
            lblDistance.Text = "Distance";
            lblCreatedDate.Text = "Created Date";
            lblLastUpdate.Text = "Last Update";
            pnlCreatedDate.Visible = false;
        }

        private void LoadSettings()
        {
            
        }

        private void LoadParams()
        {
           
        }


        #region Server Validation

        protected void cusUserName_ServerValidate(object source, ServerValidateEventArgs args)
        {
            this.UserName = txtUserName.Text.Trim();
            int len = this.UserName.Length;
            if (len == 0)
            {
                cusUserName.ErrorMessage = "Enter User name.";
                args.IsValid = false;
            }
            else if (len < 4 || len > 15)
            {
                cusUserName.ErrorMessage = "User name 4-15 character.";
                args.IsValid = false;
            }
            else
            {
                // check user
                bool isValidUser = Users.CheckUser(this.UserName);
                if (isValidUser)
                {
                    cusUserName.ErrorMessage = "User name <b>" + this.UserName + "</b> in used.";
                    args.IsValid = false;
                }
                else
                    args.IsValid = true;
            }
        }

        protected void cusPassword_ServerValidate(object source, ServerValidateEventArgs args)
        {
            this.Password = txtPassword.Text.Trim();
            int len = this.Password.Length;
            if (len == 0)
            {
                cusPassword.ErrorMessage = "Enter Password.";
                args.IsValid = false;
            }
            else if (len < 4 || len > 15)
            {
                cusPassword.ErrorMessage = "Password 4-15 character.";
                args.IsValid = false;
            }
        }

        #endregion


        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.cusUserName.ServerValidate += new ServerValidateEventHandler(cusUserName_ServerValidate);
            this.cusPassword.ServerValidate += new ServerValidateEventHandler(cusPassword_ServerValidate);
            this.btnSave.Click += new EventHandler(btnSave_Click);
        }

        #endregion
    }
}
